<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
  >
    <el-form ref="formAdd" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="标签名称" prop="tag_name">
        <el-input
          v-model="form.tag_name"
          size="mini"
          class="input-one"
          clearable
          placeholder="请输入标签名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="tag_desc">
        <el-input
          v-model="form.tag_desc"
          size="mini"
          type="textarea"
          :rows="2"
          class="input-one"
          clearable
          placeholder="描述"
        ></el-input>
      </el-form-item>
      <el-divider />
      <mk-get-button @save="save" @close="handleClose" />
    </el-form>
  </el-dialog>
</template>

<script>
import MkBackList from "@/components/common/mk-back-list";
import { dirTagsCreate, dirTagsUpdate } from "@/api/modules/sys.tags.api";

export default {
  name: "get-tags-info",
  components: { MkBackList },
  props: {
    updateData: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  data() {
    return {
      dialogTitle: "",
      type: null,
      dialogVisible: false,
      commonKey: 0,
      form: {},
      isAdd: true,
      rules: {
        tag_name: [
          { required: true, message: "请输入标签名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    open(type, typeName, data) {
      this.resetForm();
      this.type = type;
      this.dialogTitle = typeName;
      this.dialogVisible = true;
      if (data) {
        this.form = data;
      }
    },
    save() {
      this.$refs["formAdd"].validate((valid) => {
        if (valid) {
          let _FUC = "";
          _FUC = this.type == "create" ? dirTagsCreate : dirTagsUpdate;
          _FUC(this.form)
            .then((res) => {
              this.$toast.success(
                this.type == "create" ? "添加成功！" : "修改成功！"
              );
              this.dialogVisible = false;
              this.$emit("success");
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.dialogVisible = false;
        })
        .catch((_) => {});
    },
    resetForm() {
      this.form = {};
    },
  },
};
</script>

<style lang="scss" scoped></style>
